<?php

namespace app\controller;
use app\hospital\model\doctor;
use app\hospital\model\doctoruser;
use app\model\information;
use app\model\user;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        $key=Request::param('key');
        $d=new information();
        if(!empty($key)){
            $data=$d->search();
        }else{
            $data=$d->paginate(5);
        }
        View::assign(['data'=>$data,'role'=>Session::get('role'),'name'=>Session::get('name')]);
        return View::fetch();
    }
    function list(){
        $phone=Session::get('name');
        $d=new information();
        $data=$d->where('phone','=',$phone)->find();
        View::assign(['data'=>$data,'role'=>Session::get('role'),'name'=>Session::get('name')]);
        return View::fetch();
    }
    function login()
    {
        $m=Request::method();
        if($m=="GET"){
            return View::fetch();
        }else{
            $u=Request::param('user');
            $p=Request::param('pwd');
            $user=user::where(['name'=>$u,'password'=>$p])->find();
            if($user){
                Session::set('num',$user['number']);
                Session::set('name',$user['name']);
                Session::set('role',$user['role']);
                Session::set('pwd',$user['password']);
                return '登录成功';
            }else{
                return '用户名或密码有误，登录不成功';
            }
        }
    }
    function logout()
    {
        Session::delete('name');
        Session::delete('role');
        Session::delete('pwd');
        Session::delete('num');
        return redirect('/index.php/index/login');
    }
    function register()
    {
        $p=Request::post();
        if($p==null){
            return View::fetch();
        }
        else{
            $res=Db::table('user')->insert($p);
            if(!empty($res)){
                return "注册成功";
            }else{
                return "注册失败";
            }
        }

    }
    function update(){
        $data=Request::post();
        $d=new information();
        $res=$d->update($data);
//        $res=Db::table('doctor')->update($data);
        if(!empty($res)){
            return 1;
        }else{
            return 0;
        }
//        return ($res);
    }
    function edit(){
        $id=Request::get('id');
        $d=new information();
//        $data=$d->find($id);
        $data=Db::table('information')->where('num',$id)->find();
        return json($data);
    }
    function edit11(){
        $id=Request::get('id');
        $d=new user();
//        $data=$d->find($id);
        $data=$d->where('number','=',$id)->find();
        return json($data);
    }
    function update11(){
        $data=Request::post();
        $d=new user();
        $res=$d->where('number',$data['number'])->update($data);
//        $res=Db::table('doctor')->update($data);
        if(!empty($res)){
            return 1;
        }else{
            return 0;
        }
//        return ($res);
    }
    public function del(){
        $id=Request::param('id');
        $d=new information();
        $res=$d->where('num',$id)->delete();
        if($res){
            return 1;
        }else{
            return 0;
        }
    }
    public function insert(){
        $p=Request::post();
        $d=new information();
        $res=$d->insert($p);
//        $res=Db::table('subjecttype')->insert($p);
        if($res){
            return 1;
        }else{
            return 0;
        }

    }
    function mycenter(){
        $phone=Session::get('name');
        $d=new information();
        $data=$d->where('phone','=',$phone)->paginate();
        View::assign(['data'=>$data,'role'=>Session::get('role'),'num'=>Session::get('num'),'name'=>Session::get('name')]);
        return View::fetch();
    }

    function updatepwd(){
        return View::fetch();
    }
    function updatep(){
        $p=Request::param();
        if($p['password1']!=Session::get('pwd')){return "原密码有误";}
        if($p['password']!=$p['password2']){return "两次密码输入不一致";}
        if($p['password1']==Session::get('pwd')&&$p['password']==$p['password2']){
            $d=new user();
            $res=$d->where('number','=',Session::get('num'))->update(['password'=>$p['password']]);
            if(!empty($res)){
                return 1;
            }else{
                return 0;
            }
        }
    }
}
